﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class CardWH : DevExpress.XtraEditors.XtraForm
    {
        private SVC.client.maintain m;
        private BindingList<SVC.client.View_BlankCards> bcs;
        private string phone;
        private int? did;
        private SVC.client.CardOpen_M cm;
        private SVC.client.CardOpen_D cd;
        /// <summary>
        /// 维护
        /// </summary>
        /// <param name="phone">号码</param>
        /// <param name="whtype">维护类型</param>
        public CardWH(string _phone,int? _did)
        {
            phone = _phone;
            if (string.IsNullOrWhiteSpace(phone))
            {
                Comm.ShowAlert("电话号码不能为空！");
                this.Close();
            }
            if (_did!=null)
            {
                did = _did;
            }
            InitializeComponent();
        }

        private void CardWH_Load(object sender, EventArgs e)
        {
            te_phone.Text = phone;
            bcs = my.bw.View_BlankCards_List(my.ACID);
            m = new SVC.client.maintain();
            lue_Client.Properties.DataSource = my.bw.View_Client_lite_List(false, my.ACID);
            lue_Client.Properties.DisplayMember = "ClientName";
            lue_Client.Properties.ValueMember = "ClientID";
            lue_yys.Properties.DataSource = my.bw.View_Client_lite_List(true, my.ACID);
            lue_yys.Properties.DisplayMember = "ClientName";
            lue_yys.Properties.ValueMember = "ClientID";
            
            dateEdit1.DateTime = DateTime.Now;
            if (did!=null)
            {
                cd = my.bw.CardOpen_DBydid_Get(did.Value, my.ACID);
                cm = my.bw.CardOpen_MBypid_Get(cd.pid, my.ACID);
                te_opencard_pid.Text = cm.pid;
                lue_Client.EditValue = cm.ClientID;
                lue_Client.Properties.ReadOnly = true;
                ce_stopresume.Checked = true;
                ce_ChangeCard.Checked = true;
                be_newcno.Text = cd.cno;
                be_newcno.Properties.ReadOnly = true;
            }
            //SVC.client.View_numphone_Full f = my.bw.View_numphone_Full_List("", te_phone.Text, "", "", my.ACID)[0];

        }


        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            te_pid.Text = my.bw.getBillNO(my.emp_lite.shopcode, dateEdit1.DateTime, 7, my.ACID);
            m.pid = te_pid.Text;
            m.Pdate = dateEdit1.DateTime;
        }


        private void lue_cno_EditValueChanged(object sender, EventArgs e)
        {
            if (lue_Client.EditValue!=null)
            {
                SVC.client.View_cards vc = lue_Client.EditValue as SVC.client.View_cards;
                SVC.client.View_BlankCards bc = bcs.Single(p => p.GoodsCode == vc.GoodsCode);
                sp_s.EditValue = bc.outprice;
            }
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void ce_stop_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[0].PageEnabled = ce_stop.Checked;
            xtraTabControl1.SelectedTabPageIndex = 0;
            recalc();
        }

        void recalc()
        {
            decimal jes = 0;
            decimal jez = 0;
            if (xtraTabPage1.PageEnabled)
            {
                jes += Convert.ToDecimal(sps1.EditValue);
                jez += Convert.ToDecimal(spz1.EditValue);
            }
            if (xtraTabPage2.PageEnabled)
            {
                jes += Convert.ToDecimal(sps2.EditValue);
                jez += Convert.ToDecimal(spz2.EditValue);
            }
            if (xtraTabPage3.PageEnabled)
            {
                jes += Convert.ToDecimal(sps3.EditValue);
                jez += Convert.ToDecimal(spz3.EditValue);
            }
            if (xtraTabPage4.PageEnabled)
            {
                jes += Convert.ToDecimal(sps4.EditValue);
                jez += Convert.ToDecimal(spz4.EditValue);
            }
            if (xtraTabPage5.PageEnabled)
            {
                jes += Convert.ToDecimal(sps5.EditValue);
                jez += Convert.ToDecimal(spz5.EditValue);
            }
            if (xtraTabPage6.PageEnabled)
            {
                jes += Convert.ToDecimal(sps6.EditValue);
                jez += Convert.ToDecimal(spz6.EditValue);
            }
            if (xtraTabPage7.PageEnabled)
            {
                jes += Convert.ToDecimal(sps7.EditValue);
                jez += Convert.ToDecimal(spz7.EditValue);
            }
            if (xtraTabPage8.PageEnabled)
            {
                jes += Convert.ToDecimal(sps8.EditValue);
                jez += Convert.ToDecimal(spz8.EditValue);
            }
            sp_s.EditValue = jes;
            sp_z.EditValue = jez;
        }

        private void ce_ChangeCard_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[2].PageEnabled = ce_ChangeCard.Checked;
            xtraTabControl1.SelectedTabPageIndex = 2;
            recalc();
        }

        private void ce_stopresume_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[1].PageEnabled = ce_stopresume.Checked;
            xtraTabControl1.SelectedTabPageIndex = 1;
            recalc();
        }

        private void ce_Loss_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[3].PageEnabled = ce_Loss.Checked;
            xtraTabControl1.SelectedTabPageIndex = 3;
            recalc();
        }

        private void ce_LossResume_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[4].PageEnabled = ce_LossResume.Checked;
            xtraTabControl1.SelectedTabPageIndex = 4;
            recalc();
        }

        private void ce_ChangePwd_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[5].PageEnabled = ce_ChangePwd.Checked;
            xtraTabControl1.SelectedTabPageIndex = 5;
            recalc();
        }

        private void ce_ChangeInfo_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[6].PageEnabled = ce_ChangeInfo.Checked;
            xtraTabControl1.SelectedTabPageIndex = 6;
            recalc();
        }

        private void ce_transfer_CheckedChanged(object sender, EventArgs e)
        {
            xtraTabControl1.TabPages[7].PageEnabled = ce_transfer.Checked;
            xtraTabControl1.SelectedTabPageIndex = 7;
            recalc();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!dvpM.Validate()) return;
            if (xtraTabPage3.PageEnabled)
            {
                if (!dvp3.Validate())
                {
                    xtraTabPage3.Select();
                    return;
                }
            }
            if (xtraTabPage6.PageEnabled)
            {
                if (!dvp6.Validate())
                {
                    xtraTabPage6.Select();
                    return;
                }
            }
            if (xtraTabPage7.PageEnabled)
            {
                if (!dvp7.Validate())
                {
                    xtraTabPage7.Select();
                    return;
                }
            }
            if (xtraTabPage8.PageEnabled)
            {
                if (!dvp8.Validate())
                {
                    xtraTabPage8.Select();
                    return;
                }
            }
            m.clientID = new Guid(lue_Client.EditValue.ToString());
            m.gysID = new Guid(lue_yys.EditValue.ToString());
            m.custom1 = te_cust1.Text.Trim();
            m.custom2 = te_cust2.Text.Trim();
            m.opencardpid = te_opencard_pid.Text.Trim();
            m.F_balance = Convert.ToDecimal(sp_z.EditValue);
            m.F_discount = 0;
            m.F_isfinished = false;
            m.F_total = Convert.ToDecimal(sp_z.EditValue);
            m.fkd = "";
            m.ischeck = true;
            m.Pdate = dateEdit1.DateTime;
            m.phone = te_phone.Text.Trim();
            m.pid = te_pid.Text.Trim();
            m.S_balance = Convert.ToDecimal(sp_s.EditValue);
            m.S_discount = 0;
            m.S_isfinished = false;
            m.S_total = Convert.ToDecimal(sp_s.EditValue);
            m.skd = "";
            if (xtraTabPage1.PageVisible)
            {
                SVC.client.maintian_D d1 = new SVC.client.maintian_D();
                d1.isfinished = true;
                d1.pid = m.pid;
                d1.tid = 1;
                d1.yf = Convert.ToDecimal(spz1.EditValue);
                d1.ys = Convert.ToDecimal(sps1.EditValue);
                m.maintian_D.Add(d1);
            }
            if (xtraTabPage2.PageVisible)
            {
                SVC.client.maintian_D d2 = new SVC.client.maintian_D();
                d2.isfinished = true;
                d2.pid = m.pid;
                d2.tid = 2;
                d2.yf = Convert.ToDecimal(spz2.EditValue);
                d2.ys = Convert.ToDecimal(sps2.EditValue);
                m.maintian_D.Add(d2);
            }
            if (xtraTabPage3.PageVisible)
            {
                SVC.client.maintian_D d3 = new SVC.client.maintian_D();
                d3.isfinished = true;
                d3.pid = m.pid;
                d3.tid = 3;
                d3.yf = Convert.ToDecimal(spz3.EditValue);
                d3.ys = Convert.ToDecimal(sps3.EditValue);
                d3.newcno = be_newcno.Text;
                m.maintian_D.Add(d3);
            }
            if (xtraTabPage4.PageVisible)
            {
                SVC.client.maintian_D d4 = new SVC.client.maintian_D();
                d4.isfinished = true;
                d4.pid = m.pid;
                d4.tid = 4;
                d4.yf = Convert.ToDecimal(spz4.EditValue);
                d4.ys = Convert.ToDecimal(sps4.EditValue);
                m.maintian_D.Add(d4);
            }
            if (xtraTabPage5.PageVisible)
            {
                SVC.client.maintian_D d5 = new SVC.client.maintian_D();
                d5.isfinished = true;
                d5.pid = m.pid;
                d5.tid = 5;
                d5.yf = Convert.ToDecimal(spz5.EditValue);
                d5.ys = Convert.ToDecimal(sps5.EditValue);
                m.maintian_D.Add(d5);
            }
            if (xtraTabPage6.PageVisible)
            {
                SVC.client.maintian_D d6 = new SVC.client.maintian_D();
                d6.isfinished = true;
                d6.pid = m.pid;
                d6.tid = 6;
                d6.yf = Convert.ToDecimal(spz6.EditValue);
                d6.ys = Convert.ToDecimal(sps6.EditValue);
                d6.newpwd = te_newpwd.Text.Trim();
                m.maintian_D.Add(d6);
            }
            if (xtraTabPage7.PageVisible)
            {
                SVC.client.maintian_D d7 = new SVC.client.maintian_D();
                d7.isfinished = true;
                d7.pid = m.pid;
                d7.tid = 7;
                d7.yf = Convert.ToDecimal(spz7.EditValue);
                d7.ys = Convert.ToDecimal(sps7.EditValue);
                d7.linktel = te_phone.Text;
                m.maintian_D.Add(d7);
            }
            if (xtraTabPage7.PageVisible)
            {
                SVC.client.maintian_D d8 = new SVC.client.maintian_D();
                d8.isfinished = true;
                d8.pid = m.pid;
                d8.tid = 8;
                d8.yf = Convert.ToDecimal(spz8.EditValue);
                d8.ys = Convert.ToDecimal(sps8.EditValue);
                d8.newhtno = textEdit3.Text;
                d8.newusername = textEdit2.Text;
                d8.newuserIDcard = textEdit1.Text;
                m.maintian_D.Add(d8);
            }
            if (m.maintian_D.Count == 0)
            {
                Comm.ShowErorr("维护操作未选择！");
                return;
            }
            //未完待服务端方法

        }

        private void sps1_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz1_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps2_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz2_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps3_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz3_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps4_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz4_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps5_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz5_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps6_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz6_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps7_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz7_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void sps8_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }

        private void spz8_EditValueChanged(object sender, EventArgs e)
        {
            recalc();
        }
    }
}